package com.mystudy.concurrent.threadLocal;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO: 自定义ThreadLocal
 *
 * @author acy
 * @since 2019/05/29 18:43
 */
public class MyThreadLocal<T> {
	private final Map<Thread,T> storage=new HashMap<>();

	public void set(T t){
		synchronized (this){
			Thread key = Thread.currentThread();
			storage.put(key,t);
		}
	}

	public T get(){
		synchronized (this){
			Thread key = Thread.currentThread();
			T value = storage.get(key);
			if (value==null){
				return initialValue();
			}
			return value;
		}
	}

	private T initialValue() {
		return null;
	}

}
